namespace Guia.Elements
{
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Elements.PatternServants;
    using Guia.Locator;

    /// <summary>
    /// This class represents an expander. An expander can be expanded or collapsed
    /// </summary>
    public class Expander : UiElement, IExpandCollapse
    {
        /// <summary>
        /// Represents a helper-servant for the ExpandCollapse-pattern.
        /// </summary>
        private readonly IExpandCollapse expandCollapseServant;

        /// <summary>
        /// Initializes a new instance of the <see cref="Expander"/> class.
        /// </summary>
        /// <param name="automationElement">
        /// The automation element.
        /// </param>
        public Expander(AutomationElement automationElement) : base(automationElement)
        {
            this.expandCollapseServant = new ExpandCollapseImpl(this);
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.Group).And(SearchCondition.ByClassName("Expander")); }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this expander is expanded or not.
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception may be thrown when the element is disabled.</exception>
        public bool IsExpanded
        {
            get { return this.expandCollapseServant.IsExpanded; }
            set { this.expandCollapseServant.IsExpanded = value; }
        }

        /// <summary>
        /// Expands the control.
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception is thrown when the element is disabled.</exception>
        public void Expand()
        {
            this.expandCollapseServant.Expand();
        }

        /// <summary>
        /// Collapses the control
        /// </summary>
        /// <exception cref="OperationNotSupportedException">This exception is thrown when the element is disabled.</exception>
        public void Collapse()
        {
            this.expandCollapseServant.Collapse();
        }
    }
}